package com.zxc.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxc.project.base.BaseRequestEntity;
import com.zxc.project.base.BaseResponseEntity;
import com.zxc.project.pojo.query.VarietyQuery;
import com.zxc.project.request.SaveOfUpdateVarietyRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zxc.project.base.BaseController;
import com.zxc.project.base.BasePage;
import com.zxc.project.config.GlobalConfig;
import com.zxc.project.pojo.Variety;
import com.zxc.project.service.VarietyService;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/variety")
public class VarietyController extends BaseController {

    @Resource
    private GlobalConfig globalConfig;

    @Resource
    private VarietyService varietyService;

    @GetMapping("/test")
    public ResponseEntity<String> test() {
        return new ResponseEntity<>(globalConfig.getName(), HttpStatus.OK);
    }

    @PostMapping("/save")
    public BaseResponseEntity<Long> save(@Valid @RequestBody BaseRequestEntity<SaveOfUpdateVarietyRequest> requestEntity) {
        SaveOfUpdateVarietyRequest saveOfUpdateVarietyRequest = requestEntity.getData();
//        Variety variety = getPojo(Variety.class, saveOfUpdateVarietyRequest, true);
        Variety variety = saveOfUpdateVarietyRequest.getPojo(true);
        varietyService.save(variety);
        return success(variety.getId());
    }

    @GetMapping("/page")
    public BaseResponseEntity<IPage<Variety>> page(@Valid @RequestBody BaseRequestEntity<BasePage<Variety, VarietyQuery>> requestEntity) {
        Page<Variety> page = requestEntity.getData().getPage();
        VarietyQuery varietyQuery = requestEntity.getData().getParam();
        return success(varietyService.page(page, varietyQuery));
    }

}
